<?php Yii::app()->clientScript->registerScriptFile(Helper::getUrl('js/tiny_mce/tiny_mce.js'))?>
<script type="text/javascript">
    tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,syntaxhl",
//		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,insertCode,syntaxHighlighter,uploadImage,insertMusic",
//		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,insertCode,syntaxHighlighter,uploadImage,insertMusic",

		// Theme options
		theme_advanced_buttons1 : "formatselect,forecolor,backcolor,bold,italic,underline,strikethrough,bullist,numlist,justifyleft,justifycenter,justifyright,syntaxhlundo,redo,outdent,indent,removeformat,link,unlink,image,uploadImage,media,insertCode,syntaxHighlighter,code,syntaxhl",
		theme_advanced_buttons2 : null,
		theme_advanced_buttons3 : null,
		theme_advanced_buttons4 : null,
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		convert_fonts_to_spans:true,
		remove_trailing_nbsp:true,
		convert_newlines_to_brs:false,
		force_br_newlines:false,
		force_p_newlines:false,
		remove_linebreaks:false ,
		verify_html:true,
		relative_urls:false,
		remove_script_host: false,
		paste_preprocess: function(pl, o) {
			o.content = o.content.replace(/[\n\r\t]/g, '');
			o.content = o.content.replace(/<br\s\/><br\s\/>/g, '</p><p>');
			o.content = o.content.replace(/<br><br>/g, '</p><p>');
		},
		extended_valid_elements:"pre[name|class],style",
		content_css:"<?php echo Helper::getUrl('css/mce.css')?>",
		//cleanup_callback:"myCustomCleanup",
		handle_event_callback:"MCECheckIndent",
		cleanup : true
	});

		function MCECheckIndent(e) {
			if(e.type == 'keydown' && e.keyCode == 9)
			{
			tinyMCE.execCommand('mceInsertContent',false,'　　');
			return false;
			}
		}

		function InsertToEditor(content){
			tinyMCE.execCommand('mceInsertContent',false,content);
		}

(function($){
	$(function(){
        var data = $('#post-form').serialize();
        setInterval(function(){
            $('#post-form #elm1').text(tinyMCE.activeEditor.getContent());
            var serialize = $('#post-form').serialize();
            if (data == serialize) return;
            data = serialize;
            $("#cache img").show();
            $.ajax({
                url:"<?php echo $this->createUrl('tempcache', array('id' => $model->id))?>",
                type:"post",
                data:data,
                dataType:'json',
                success:function(data){
                    $("#cache span").html("Saved at " + data.time);
                    $("#cache img").hide();
                    $("#cache a").show();
                }
            });
        }, 5000);
        if ($("#cache span").html() == '') {$("#cache a").hide();}
	})
})(jQuery)

function cleanConfirm(){
    return confirm("确定要清除缓存吗？") ? true : false;
}
</script>

<div class="form">
<?php $form=$this->beginWidget('ActiveForm', array(
	'id'=>'post-form',
	'enableAjaxValidation'=>false,
)); ?>

<?php echo $form->errorSummary($model); ?>

<div class="row">
	<?php echo $form->dropDownList($model, 'cid', CHtml::listData($model->categoryData->findAll(), 'id', 'name'))?>
    <?php echo $form->textfield($model, 'title', array('class' => 'title')); ?>

    <div id="cache"><img src="<?php echo Helper::getUrl('images/loading.gif')?>">
    <span><?php if (!empty($time)) echo 'Has saved at ' . $time;?></span>
    <?php if($model->id):?>
    <?php echo CHtml::link('清除', array('update', 'id' => $model->id, 'ct' => 1), array('onclick' => 'return cleanConfirm()'))?>
    <?php else:?>
    <?php echo CHtml::link('清除', array('create', 'ct' => 1), array('onclick' => 'return cleanConfirm()'))?>
    <?php endif;?>
    </div>
</div>

<div class="row">
	<?php echo $form->textArea($model,'content',array('id' => 'elm1', 'style' => 'height:450px;width:100%')); ?>
	<?php echo $form->error($model,'content_markdown'); ?>
</div>

<div class="row">
	<?php echo $form->labelEx($model,'tag'); ?>
	<?php echo $form->textField($model,'tag',array('maxlength'=>64, 'style' => 'width:200px')); ?>
	<?php echo $form->error($model,'tag'); ?>
</div>

<div class="row buttons">
	<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

<?php $this->endWidget(); ?>

</div><!-- form -->